package com.librariy.util;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.text.Layout;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.AlignmentSpan;
import android.text.style.BackgroundColorSpan;
import android.text.style.DynamicDrawableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StyleSpan;
public class TextSpanBuilder extends SpannableStringBuilder {
    public TextSpanBuilder() {
        super();
    }
    /**
     * @param mTextStyle An integer constant describing the style for this span. Examples
     *              include bold, italic, and normal. Values are constants defined
     *              in {@link Typeface}.
     */
    public TextSpanBuilder append(String text, int mTextStyle, float mRelativeTextSize, int mTextColor, int mBgColor) {
        return this.append(text, mTextStyle, mRelativeTextSize, mTextColor, mBgColor, null);
    }
    /**
     * @param mTextStyle An integer constant describing the style for this span. Examples
     *              include bold, italic, and normal. Values are constants defined
     *              in {@link Typeface}.
     */
    public TextSpanBuilder append(String text, int mTextStyle, float mRelativeTextSize, int mTextColor, int mBgColor,Layout.Alignment align) {
        SpannableString styleString = new SpannableString(text);
        if (mTextStyle != 0)
            styleString.setSpan(new StyleSpan(mTextStyle), 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        if (mRelativeTextSize  != 0)
            styleString.setSpan(new RelativeSizeSpan(mRelativeTextSize), 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        if (mTextColor  != 0)
            styleString.setSpan(new ForegroundColorSpan(mTextColor), 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        if (mBgColor  != 0)
            styleString.setSpan(new BackgroundColorSpan(mBgColor), 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        if (align  != null)
            styleString.setSpan(new AlignmentSpan.Standard(align), 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);        
        super.append(styleString);
        return this;
    }
    /**
     * verticalAlignment- {@link DynamicDrawableSpan#ALIGN_BASELINE}
     * */
    public TextSpanBuilder img(Drawable mDrawable) {
        if (mDrawable==null){
            return this;
        }
        SpannableString styleString = new SpannableString("M");
        ImageSpan mImageSpan=new ImageSpan(mDrawable);
        styleString.setSpan(mImageSpan,0,styleString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        super.append(styleString);
        return this;
    }

    @Override
    public TextSpanBuilder append(CharSequence text) {
        super.append(text);
        return this;
    }

    @Override
    public TextSpanBuilder append(CharSequence text, int start, int end) {
        super.append(text);
        return this;
    }

    @Override
    public TextSpanBuilder append(char text) {
        super.append(text);
        return this;
    }

    public static class MuImageSpan extends ImageSpan{
        public MuImageSpan(Drawable d) {
            super(d);
        }
        @Override
        public int getSize(Paint paint, CharSequence text, int start, int end,Paint.FontMetricsInt fontMetricsInt) {
            Drawable drawable = getDrawable();
            Rect rect = drawable.getBounds();
            return rect.right;
        }
        @Override
        public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
            Drawable drawable = getDrawable();
            Rect rect = drawable.getBounds();
            canvas.save();
            if(rect.height()<(bottom-top)){
                //y=(bottom-top)/2-rect.height()/2;
            }
            canvas.translate(x, y);
            drawable.draw(canvas);
            canvas.restore();
        }
    }
}